// router.js
import {
	RouterMount,
	createRouter
} from 'uni-simple-router';

import store from '@/store'
import {
	getToken
} from '@/common/auth.js'

const router = createRouter({
	encodeURI: false,
	platform: process.env.VUE_APP_PLATFORM,
	routes: ROUTES
});

const NON_TOKEN_WHITE_ROUTER = ["Register", "Login", "ForgotPassword"]

//全局路由前置守卫
router.beforeEach((to, from, next) => {
	const hasToken = getToken()
	// 1.没有token，则去登录
	if (!hasToken) {
		console.log("没有Token，跳转到Login")
		if (NON_TOKEN_WHITE_ROUTER.includes(to.name)) {
			// console.log("没有Token，但是路由是白名单")
			return next()
		}
		next({
			name: 'Login',
			"NAVTYPE": 'replace'
		})
		return
	}
	// console.log("to.name", to.name)
	// init app data
	const appData = store.getters.appData
	// console.log('appData', appData)
	if (!appData) {
		store.dispatch('user/initApp')
	}
	// b. 有token,但是跳转Login等界面，需要重置到index
	if (['Login', 'Default'].includes(to.name)) {
		// console.log("sa")
		next({
			name: 'BarSN'
		})
		return
	}
	next();
});
// 全局路由后置守卫
router.afterEach((to, from) => {
	// console.log('跳转结束')
})

function handlerToken() {

}

export {
	router,
	RouterMount
}